<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/25 0025
 * Time: 下午 2:46
 */
namespace Api\Controller;

class MemberForumController extends ApiController{

    public function __construct(){
        parent::__construct();
        if(!$this->user_info){
            $this->error($this->error,999);
            exit;
        }

        $forums = S('forum_category_cache');
        if(empty($forums)){
            A('Forum')->cache(false);
        }
    }

    public function index(){
        $forums = S('forum_category_cache');
        $type = I('type','');

        $threads = $this->threads();
        $posts = $this->posts();

        if($type == 'thread'){
            $return = [
                'threads'   => $threads,
            ];
        }elseif($type == 'post'){
            $return = [
                'posts'   => $posts,
            ];
        }else{
            $return  = [
                'threads'   =>  $threads,
                'posts'     =>  $posts,
                'forums'    =>  $forums,
            ];
        }

        $this->success($return, '');
    }

    //我的主题
    public function threads(){

        //我的主题
        $where = array();
        $where['author_id'] = $this->user_info['id'];
        $where['display'] = 1;
        $fid = I('fid',0,'intval');
        $keyword = I('keyword','','addslashes');
        if(!empty($fid)){
            $where['fid'] = $fid;
        }
        if(!empty($keyword)){
            $keyword = urldecode($keyword);
            $where['subject'] = array('like', "%{$keyword}%");
        }

        $threads = M('ForumThread')->where($where)->order('lastpost desc,id desc')->page($this->page, $this->page_size)->select();

        $forums = S('forum_category_cache');
//        $forum_html = $this->treeCategory($forums, I('fid'));
        foreach($threads as $k=>$v){
            if(isset($forums[$v['fid']])){
                $threads[$k]['fname'] = $forums[$v['fid']]['name'];
            }
            $threads[$k]['createTime'] = date('Y-m-d', $v['create_time']);
        }

        return $threads;
    }

    //我的帖子
    public function posts(){
        $where = array();
        $where['A.uid'] = $this->user_info['id'];
        $where['A.display'] = 1;
        $where['A.is_first'] = 0;

        //搜索
        $fid = I('fid',0,'intval');
        $keyword = I('keyword','','addslashes');
        if(!empty($fid)){
            $where['A.fid'] = $fid;
        }
        if(!empty($keyword)){
            $keyword = urldecode($keyword);
            $where['A.message'] = array('like', "%{$keyword}%");
        }
        //end

        $threads = M('ForumPost')->alias('A')->field('A.*,B.subject,B.author')
            ->join(C('DB_PREFIX').'forum_thread B ON A.tid = B.id','left')
            ->where($where)->order('lastpost desc,id desc')->page($this->page, $this->page_size)->select();

        $forums = S('forum_category_cache');
        foreach($threads as $k=>$v){
            if(isset($forums[$v['fid']])){
                $threads[$k]['fname'] = $forums[$v['fid']]['name'];
            }
            $threads[$k]['createTime'] = date('Y-m-d', $v['create_time']);
        }

        return $threads;
    }
}